$(document).ready(function(){
    // create dialog to handel role form
    var roleViewDialog = new DialogBox($('#role_form_view'))
    /*
     * 1. 
     * process role form
     */
    var processRoleForm = function(){
        MAX.blockPage()
        MAX.ajax('GET', $(this).attr('href'),'', function(res){
            MAX.unBlockPage()
            res = $.parseJSON(res)
            roleViewDialog.html(res.html)
            roleViewDialog.open();
        })
        return false;
    }
    /*
     * 1.1
     * Add new role
     */
    $('#add_new_role').click(processRoleForm)
      
    /*
     * 1.2
     * Edit role form
     */
    $('.roles_list').on('click','.edit_role',processRoleForm)
    
    /*
     * 1.2. submit new role form
     */
    $('#role_form_view').on('submit','#role_form',function(){
        roleViewDialog.html('<p><center>'+__('Please wait')+'.....<div class="wait">&nbsp;</div></center></p>')
        var data = $(this).serialize()
        var requestUrl = $(this).attr('action') 
        /*
         * send ajax request 
         */
        MAX.ajax('POST', requestUrl, data, function(response){
            var res = jQuery.parseJSON(response)
            if(res.validFlag == true)
            {
                roleViewDialog.close()
                $('.roles_list').html(res.html)
            }
            else
            {
                roleViewDialog.html(res.html) 
            }
        })
       
        return false;
    })
    
    /*
     * 1.4. delete role
     */
    $('.roles_list').on('click','.delete_role',function(){
        var requestUrl = $(this).attr('href')
        var toThis = this
        MAX.confirmDialog(__('Delete role')+' '+$(this).attr('val'), function(){
            MAX.blockPage()
            MAX.ajax('POST', requestUrl, '', function(response){
                var res = jQuery.parseJSON(response)
                if(res.deleteFlag == true)
                {
                    MAX.unBlockPage()
                    $('.roles_list').html(res.html) 
                }
                else
                {
                    MAX.confirmDialog(res.msg)     
                }
            })
        })
        
        return false
    })
    
    /*
     * 1.5. set role default
     */
    $('.roles_list').on('click','.default_role',function(){
        var requestUrl = $(this).parent().find('input[type=hidden]').val()
        var toThis = this
        $.ajax({
            url:requestUrl,
            success:function(html){
                $('.default_role').attr('checked', false);
                $(toThis).attr('checked', true);
            }
        })
        return false;
    })
        
    /*
     * 1.6. change role type
     */
    $('.roles_list').on('click','.type_role',function(){
        return ajaxChangeCheckbox(this)
    })
    
    /*
     * 1.7. change role categories
     */
    $('.roles_list').on('click','.need_categories',function(){
        return ajaxChangeCheckbox(this)
    })
    
    
    /*
     * 1.8. change role tasks
     */
    $('.roles_list').on('click','.changeroleTask',function(){
        return ajaxChangeCheckbox(this)
    })
    
    function ajaxChangeCheckbox(toThis)
    {
        var requestUrl = $(toThis).parent().find('input[type=hidden]').val()
        $('#dialog').dialog({
            modal:true
        });
        $.ajax({
            url:requestUrl,
            success:function(html){
            }
        })
    }
})